<? 
	
	
	define('AUDITION_INSTALLING', true);
	include('../base.php');

	$e = new Error();
	
	$page_title = 'Install Audition';
	
	if ($_POST['submit']) { 
		if (!trim($_POST['dbServer'])) {
			$e->add("You must specify a valid database server. 'localhost' (no quotes) is typically the most often supplied.");
		}
		if (!trim($_POST['dbName'])) {
			$e->add("You must specify a valid database.");
		}
		if (!$e->hasErrors()) {
			
			// attempt to connect to the database
			$res = db::connect($_POST['dbServer'], $_POST['dbUser'], $_POST['dbPassword'], $_POST['dbName']);
			if (db::isError($res)) {
				$e->add("There was an error connecting to the database specified: " . $res->get());
			}
		}				
		if (!trim($_POST['username'])) {
			$e->add("Please specify an admin username.");
		}
		
		if (!trim($_POST['password'])) {
			$e->add("Please specify an admin password.");
		}
		
		if (trim($_POST['password']) != trim($_POST['passwordConfirm'])) {
			$e->add("Your two passwords do not match.");
		}
		
		if (!trim($_POST['email'])) {
			$e->add("Please specify an email address.");
		}
		
		if (!trim($_POST['firstname'])) {
			$e->add("Please specify a first name.");
		}
		
		
		
		if (!$e->hasErrors() && !db::isError($res)) {

			// attempt to create relevant directories
			if (!file_exists(BASE_FULL_DIRECTORY . MEDIA_ORIGINALS_DIRECTORY)) {
				$dir1 = @mkdir(BASE_FULL_DIRECTORY . MEDIA_ORIGINALS_DIRECTORY);
				if (!$dir1) {
					$e->add("Error creating directory: " . MEDIA_ORIGINALS_DIRECTORY);
				}
			}
			if (!file_exists(BASE_FULL_DIRECTORY . MEDIA_INCOMING_DIRECTORY)) {
				$dir2 = @mkdir(BASE_FULL_DIRECTORY . MEDIA_INCOMING_DIRECTORY);
				if (!$dir2) {
					$e->add("Error creating directory: " . MEDIA_INCOMING_DIRECTORY);
				}
			}
		}
		
		if (!$e->hasErrors()) {
			$res = @chmod(BASE_FULL_DIRECTORY . MEDIA_ORIGINALS_DIRECTORY, 0777);
			if (!$res) {
				$e->add("Unable to set permissions on directory: " . MEDIA_ORIGINALS_DIRECTORY);
			}
			$res2 = @chmod(BASE_FULL_DIRECTORY . MEDIA_INCOMING_DIRECTORY, 0777);
			if (!$res2) {
				$e->add("Unable to set permissions on directory: " . MEDIA_INCOMING_DIRECTORY);
			}
			$res3 = @touch(BASE_FULL_DIRECTORY . MEDIA_INCOMING_DIRECTORY . '/index.html');
		}
		
		if (!$e->hasErrors()) {
		
			
			require(dirname(__FILE__).'/mysql.php');
			// returns $import[]
			
			if (count($import) > 0) { 
				foreach($import as $stmt) {
					$r = @mysql_query($stmt);
					if (!$r) {
						break;
					}
				}
			}
			
			if (!$r) {
				$e->add("Error installing Audition database: " . mysql_error());
			} else {
				// create admin user
				$username = db::sanitize_to_db($_POST['username']);
				$password = md5(trim($_POST['password']));
				$email = db::sanitize_to_db($_POST['email']);
				$birthdate = db::sanitize_to_db($_POST['birthdate']);
				$firstname = db::sanitize_to_db($_POST['firstname']);
				$lastname = db::sanitize_to_db($_POST['lastname']);
				
				$q = "insert into Users (username, password, email, birthdate, firstname, lastname, level) values ('{$username}', '{$password}', '{$email}', '{$birthdate}', '{$firstname}', '{$lastname}', 'ADMIN')";
				$r = @mysql_query($q);
				if (!$r) {
					$e->add("Error creating administrative user: " . mysql_error());
				}			
			}
		}
		
		if (!$e->hasErrors()) {
			// finally, we write the configuration file
			
			// create web directory from request_uri
			
			$webDirectory = substr($_SERVER['REQUEST_URI'], 0, strpos($_SERVER['REQUEST_URI'], AUDITION_WEB_DIRECTORY . '/system/install.php'));

			$fp = @fopen(SYSTEM_DIRECTORY . '/configuration.php', 'w+');
			$configuration = "<?php\n";
			$configuration .= "define('DB_SERVER', '{$_POST['dbServer']}');\n";
			$configuration .= "define('DB_SERVER_USERNAME', '{$_POST['dbUser']}');\n";
			$configuration .= "define('DB_SERVER_PASSWORD', '{$_POST['dbPassword']}');\n";
			$configuration .= "define('DB_DATABASE', '{$_POST['dbName']}');\n";
			$configuration .= "define('SITE_WEB_DIRECTORY', '{$webDirectory}');\n";
			$configuration .= "define('AUDITION_INSTALLED', 1);\n";
			$configuration .= "?" . ">";
			$res = @fwrite($fp, $configuration);
			@fclose($fp);
			if ($res) {
				header('Location: ../index.php');
				exit;
			}
		}
	}
	$layoutPrefix = "../";
	include(BASE_FULL_DIRECTORY . AUDITION_WEB_DIRECTORY . '/layout/header.php');
	
if (!file_exists(dirname(__FILE__).'/configuration.php')) { ?>

<div id="breadcrumb">
	Audition Installation &#62;
</div>

	<? if ($e->hasErrors()) {
		$e->outputList();
	} ?>
	
	
	<div class="item" style="padding: 20px 10px 10px 10px"><strong class="error">Important</strong>! Submitting the form below will completely erase any existing content you have in your Audition website. If you're trying to upgrade, <a href="upgrade.php">you should go here</a>.</div>
	
	<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
	<br/>
	<div class="col1">
		<h2>Database</h2>
		<p>Audition requires a MySQL database. Please provide the information for connecting to the MySQL database on your server. This information will typically be provided to you by your hosting company.</p>
		
		<table border="0" class="edit-form" cellspacing="0" cellpadding="0">
		<tr>
			<td valign="top">
			<strong>Server</strong><br/>
			<input type="text" value="<?=$_POST['dbServer']?>" class="text" name="dbServer" />
			</td>
		</tr>
		<tr>
			<td valign="top">
				<strong>User</strong><br/>
				<input type="text" value="<?=$_POST['dbUser']?>" class="text" name="dbUser" />
			</td>
		</tr>
		<tr>
			<td valign="top">
				<strong>Password</strong><br/>
				<input type="text" value="<?=$_POST['dbPassword']?>" class="text" name="dbPassword" />
			</td>
		</tr>
		<tr>
			<td valign="top">
				<strong>Database Name</strong><br/>
				<input type="text" value="<?=$_POST['dbName']?>" class="text" name="dbName" />
			</td>
		</tr>
		</table>
		<br/>
	</div>
	<div class="colspacer">&nbsp;</div>
	<div class="col2">
		<h2>Administrative User</h2>
		<p>Before you can start setting up your site, you need to create a user account that can do everything. This is typically a user named "admin" (but it doesn't have to be.). <strong>If you forget this password, you will be unable to access Audition!</strong></p>
		<table border="0" class="edit-form" cellspacing="0" cellpadding="0">
		<tr>
			<td width="50%" valign="top">
			<strong>Username</strong><br/>
			<input type="text" value="<?=$_POST['username']?>" class="text" name="username" />
			</td>
			<td width="50%" valign="top">
			<strong>Password</strong><br/>
			<input type="password" value="<?=$_POST['password']?>" class="text" name="password" />
			</td>
		</tr>
		<tr>
			<td width="50%" valign="top">
			<strong>Password (Confirm)</strong><br/>
			<input type="password" value="<?=$_POST['passwordConfirm']?>" class="text" name="passwordConfirm" />
			</td>
			<td width="50%" valign="top">
			<strong>Email Address</strong><br/>
			<input type="text" value="<?=$_POST['email']?>" class="text" name="email" />
			</td>
		</tr>
		<tr>
			<td width="50%" valign="top">
			<strong>First Name</strong><br/>
			<input type="text" value="<?=$_POST['firstname']?>" class="text" name="firstname" />
			</td>
			<td width="50%" valign="top">
			<strong>Last Name</strong><br/>
			<input type="text" value="<?=$_POST['lastname']?>" class="text" name="lastname" />
			</td>
		</tr>
		<tr>
			<td>
				<strong>Birth Date (mm/dd/yyyy)</strong><br/>
				<input type="text" value="<?=$_POST['birthdate']?>" class="text" name="birthdate" id="birthdate" />

			</td>
			<td>&nbsp;</td>
		</tr>
		</table>
	</div>
	<div style=" text-align: right"><input type="submit" name="submit" value="Install Audition" /></div>
	</form>

<? } else { ?>
	<div id="breadcrumb">
		Audition Installation &#62;
	</div>
	
	<? Error::outputDialog(null, null,'Audition has already been installed.'); ?>

<? } ?>

<? include(BASE_FULL_DIRECTORY . AUDITION_WEB_DIRECTORY . '/layout/footer.php'); ?>